package com.stox.services.watchlist.manager.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.stox.services.watchlist.manager.WatchlistEntryManager;
import com.stox.services.watchlist.manager.WatchlistManager;
import com.stox.services.watchlist.repository.WatchlistRepository;
import com.stox.shared.model.Watchlist;
import com.stox.shared.model.WatchlistEntry;

@Component
public class WatchlistManagerImpl implements WatchlistManager {

	@Autowired
	private WatchlistRepository watchlistRepository;
	
	@Autowired
	private WatchlistEntryManager watchlistEntryManager;
	
	@Override
	public List<Watchlist> findAll() {
		return watchlistRepository.findAll();
	}

	@Override
	public Long saveOrUpdate(Watchlist watchlist) {
		return watchlistRepository.saveOrUpdate(watchlist);
	}

	@Override
	public void delete(Long id) {
		watchlistRepository.delete(id);
		List<WatchlistEntry> watchlistEntries = watchlistEntryManager.findByWatchlistId(id);
		for(WatchlistEntry watchlistEntry : watchlistEntries){
			watchlistEntryManager.delete(watchlistEntry.getId());
		}
	}

}
